package com.baozhun.mall.common.base

import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.baozhun.mall.common.BR
import com.baozhun.mall.common.R
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.ext.dismissLoadingExt
import com.baozhun.mall.common.ext.showLoadingExt
import com.baozhun.mall.common.ext.showToast
import com.baozhun.mall.common.listener.ViewOnClickListener
import com.baozhun.mall.common.model.enums.MainNavigateType
import com.baozhun.mall.common.util.eventbus.EventBusUtils
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import org.greenrobot.eventbus.EventBus

/**
 * 时间　: 2019/12/21
 * 作者　: hegaojian
 * 描述　: 你项目中的Activity基类，在这里实现显示弹窗，吐司，还有加入自己的需求操作 ，如果不想用Databind，请继承
 * BaseVmActivity例如
 * abstract class BaseActivity<VM : BaseViewModel> : BaseVmActivity<VM>() {
 */
abstract class BaseActivity<VM : BaseViewModel, DB : ViewDataBinding> : BaseVmDbActivity<VM, DB>(),
    View.OnClickListener,
    ViewOnClickListener,
    OnTitleBarListener {

    abstract override fun initView(savedInstanceState: Bundle?)

    /**
     * 创建liveData观察者
     */
    override fun createObserver() {}

    /**
     * 打开等待框
     */
    override fun showLoading(message: String) {
        showLoadingExt(message)
    }

    /**
     * viewModel 对应的变量，默认为viewModel
     */
    override fun bindingVmVariable(): Int {
        return BR.viewModel
    }

    /**
     * viewModel 对应的变量，默认为viewModel
     */
    override fun bindingListenerVariable(): Int {
        return BR.listener
    }

    /**
     * 关闭等待框
     */
    override fun dismissLoading() {
        dismissLoadingExt()
    }

    /**
     * 绑定xml变量
     */
    override fun bindingVariable() {
        mDataBinding?.setVariable(bindingVmVariable(), mViewModel)
        mDataBinding?.setVariable(bindingListenerVariable(), this)
        mDataBinding?.executePendingBindings()
        var titleBar = mDataBinding?.root?.findViewById<TitleBar>(R.id.title_bar)
        titleBar?.setOnTitleBarListener(this)
    }

    override fun initData() {
        super.initData()
    }

    override fun onClick(v: View?) {

    }

    override fun onLeftClick(titleBar: TitleBar?) {
        super.onLeftClick(titleBar)
        when (titleBar?.leftIconAction) {
            TitleBar.ACTION_ICON_CLICK_MENU -> ARouter.getInstance().build(RoutePath.Common.SWIPE)
                .withTransition(R.anim.activity_open_enter, R.anim.activity_open_exit).navigation(this)

            TitleBar.ACTION_ICON_CLICK_BACK -> finish()
            else -> {
                titleBar?.leftView?.id = com.hjq.bar.R.id.title_bar_left_view
                onClick(titleBar?.leftView)
            }
        }
    }

    override fun onLeft2Click(titleBar: TitleBar?) {
        super.onLeft2Click(titleBar)
        when (titleBar?.leftIcon2Action) {
            TitleBar.ACTION_ICON_CLICK_MENU -> ARouter.getInstance().build(RoutePath.Common.SWIPE)
                .withTransition(R.anim.activity_open_enter, R.anim.activity_open_exit).navigation(this)

            TitleBar.ACTION_ICON_CLICK_BACK -> finish()
            else -> {
                titleBar?.leftView2?.id = com.hjq.bar.R.id.title_bar_left_view2
                onClick(titleBar?.leftView2)
            }
        }
    }

    override fun onRightClick(titleBar: TitleBar?) {
        super.onRightClick(titleBar)
        when (titleBar?.rightIconAction) {
            TitleBar.ACTION_ICON_CLICK_SEARCH -> BaseApplication.mEventViewModelInstance.switchTabEvent.value =
                MainNavigateType.values().indexOf(MainNavigateType.MESSAGE)

            TitleBar.ACTION_ICON_CLICK_MESSAGE -> ARouter.getInstance().build(RoutePath.Home.MESSAGE_CENTER).navigation()
            TitleBar.ACTION_ICON_CLICK_SHARE -> showToast("BaseActivity -> 分享")
            else -> {
                titleBar?.rightView?.id = com.hjq.bar.R.id.title_bar_right_view
                onClick(titleBar?.rightView)
            }
        }

    }

    override fun onRight2Click(titleBar: TitleBar?) {
        super.onRightClick(titleBar)
        when (titleBar?.rightIcon2Action) {
            TitleBar.ACTION_ICON_CLICK_SEARCH -> BaseApplication.mEventViewModelInstance.switchTabEvent.value =
                MainNavigateType.values().indexOf(MainNavigateType.MESSAGE)

            TitleBar.ACTION_ICON_CLICK_MESSAGE -> ARouter.getInstance().build(RoutePath.Home.MESSAGE_CENTER).navigation()
            TitleBar.ACTION_ICON_CLICK_SHARE -> showToast("BaseActivity -> 分享")
            else -> {
                titleBar?.rightView2?.id = com.hjq.bar.R.id.title_bar_right_view2
                onClick(titleBar?.rightView2)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBusUtils.unregister(this)
    }
}